package engine.graphics.anim
{
	public class RxFrameAnimation
	{
		protected var m_numFrames:uint;
		protected var m_frameTime:Number;
		protected var m_currentFrame:uint;
		protected var m_nextFrame:uint;
		protected var m_elapsedTime:Number;
		protected var m_frameBlendRatio:Number;
		protected var m_running:Boolean;
		
		public function RxFrameAnimation()
		{
			this.Init(1, 0);
		}
		
		public function Init(numFrames:uint, frameTime:Number) : void
		{
			this.m_numFrames = numFrames;
			this.m_frameTime = frameTime;
			this.m_currentFrame = 0;
			this.m_nextFrame = (this.m_currentFrame + 1) % this.m_numFrames;
			this.m_elapsedTime = 0;
			this.m_frameBlendRatio = 0;
			this.m_running = false;
		}
		
		public function Start() : void
		{
			this.m_running = true;
		}
		
		public function Stop() : void
		{
			this.m_running = false;
		}
		
		public function Reset() : void
		{
			this.m_currentFrame = 0;
			this.m_nextFrame = (this.m_currentFrame + 1) % this.m_numFrames;
			this.m_elapsedTime = 0;
			this.m_frameBlendRatio = 0;
		}
		
		public function Restart() : void
		{
			this.Reset();
			this.Start();
		}
		
		public function Update(time:Number) : Boolean
		{
			if (!this.m_running)
			{
				return false;
			}
			
			this.m_elapsedTime = this.m_elapsedTime + time;
			var delay:Number = this.m_elapsedTime / this.m_frameTime;
			var t:int = Math.floor(delay);
			this.m_frameBlendRatio = delay - t;
			var bol:Boolean = false;
			if (t > 0)
			{
				this.m_currentFrame = (this.m_currentFrame + t) % this.m_numFrames;
				this.m_nextFrame = (this.m_currentFrame + 1) % this.m_numFrames;
				this.m_elapsedTime = this.m_elapsedTime - t * this.m_frameTime;
				bol = true;
			}
			
			return bol;
		}
		
		public function GetCurrentFrame() : uint
		{
			return this.m_currentFrame;
		}
		
		public function GetNextFrame() : uint
		{
			return this.m_nextFrame;
		}
		
		public function GetFrameBlendRatio() : Number
		{
			return this.m_frameBlendRatio;
		}
		
		public function IsRunning() : Boolean
		{
			return this.m_running;
		}
	}
}